import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getAllRolesListNoByTenant, getAllTenantList } from './user.api';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n('system.user.fields');
const { t: common } = useI18n('common');

export const userFields = {
  username: t('username'),
  nickName: t('nickName'),
  addr: t('addr'),
  realname: t('realname'),
  avatar: t('avatar'),
  sex: t('sex'),
  birthday: t('birthday'),
  phone: t('phone'),
  orgCode: t('orgCode'),
  departIds: t('departIds'),
  status: t('status'),
  password: t('password'),
  confirmPassword: t('confirmPassword'),
  workNo: t('workNo'),
  post: t('post'),
  selectedroles: t('selectedroles'),
  selecteddeparts: t('selecteddeparts'),
  userIdentity: t('userIdentity'),
  normalUser: t('normalUser'),
  leader: t('leader'),
  email: t('email'),
  telephone: t('telephone'),

  oldpassword: t('oldpassword'),
  newPassword: t('newPassword'),
  confirmNewPassword: t('confirmNewPassword'),
};
export const columns: BasicColumn[] = [
  {
    title: userFields.username,
    dataIndex: 'username',
    width: 120,
  },
  {
    title: userFields.realname,
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: userFields.avatar,
    dataIndex: 'avatar',
    width: 120,
    customRender: render.renderAvatar,
  },
  {
    title: userFields.sex,
    dataIndex: 'sex',
    width: 80,
    sorter: true,
    customRender: ({ text }) => {
      return render.renderDict(text, 'sex');
    },
  },
  {
    title: userFields.birthday,
    dataIndex: 'birthday',
    width: 100,
  },
  {
    title: userFields.phone,
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: userFields.orgCode,
    width: 150,
    dataIndex: 'orgCodeTxt',
  },
  {
    title: userFields.departIds,
    width: 150,
    dataIndex: 'departIds_dictText',
  },
  {
    title: userFields.status,
    dataIndex: 'status_dictText',
    width: 80,
  },
];

export const recycleColumns: BasicColumn[] = [
  {
    title: userFields.username,
    dataIndex: 'username',
    width: 100,
  },
  {
    title: userFields.realname,
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: userFields.avatar,
    dataIndex: 'avatar',
    width: 80,
    customRender: render.renderAvatar,
  },
  {
    title: userFields.sex,
    dataIndex: 'sex',
    width: 80,
    sorter: true,
    customRender: ({ text }) => {
      return render.renderDict(text, 'sex');
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: userFields.username,
    field: 'username',
    component: 'JInput',
    colProps: { span: 6 },
  },
  {
    label: userFields.realname,
    field: 'realname',
    component: 'JInput',
    colProps: { span: 6 },
  },
  {
    label: userFields.sex,
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: common('chooseText') + userFields.sex,
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
  {
    label: userFields.phone,
    field: 'phone',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: userFields.status,
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'user_status',
      placeholder: common('chooseText') + userFields.status,
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: userFields.username,
    field: 'username',
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      return !!values.id;
    },
    dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_user', 'username', model, schema, true),
  },
  {
    label: userFields.password,
    field: 'password',
    component: 'StrengthMeter',
    rules: [
      {
        required: true,
      },
    ],
  },
  {
    label: userFields.confirmPassword,
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
  {
    label: userFields.realname,
    field: 'realname',
    required: true,
    component: 'Input',
  },
  {
    label: userFields.workNo,
    field: 'workNo',
    required: true,
    component: 'Input',
    dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_user', 'work_no', model, schema, true),
  },
  {
    label: userFields.post,
    field: 'post',
    required: false,
    component: 'JSelectPosition',
    componentProps: {
      rowKey: 'code',
      labelKey: 'name',
    },
  },
  {
    label: userFields.selectedroles,
    field: 'selectedroles',
    component: 'ApiSelect',
    componentProps: {
      mode: 'multiple',
      api: getAllRolesListNoByTenant,
      labelField: 'roleName',
      valueField: 'id',
    },
  },
  {
    label: userFields.selecteddeparts,
    field: 'selecteddeparts',
    component: 'JSelectDept',
    componentProps: ({ formActionType, formModel }) => {
      return {
        sync: false,
        checkStrictly: true,
        defaultExpandLevel: 2,

        onSelect: (options, values) => {
          const { updateSchema } = formActionType;
          //所属部门修改后更新负责部门下拉框数据
          updateSchema([
            {
              field: 'departIds',
              componentProps: { options },
            },
          ]);
          //所属部门修改后更新负责部门数据
          formModel.departIds && (formModel.departIds = formModel.departIds.filter((item) => values.value.indexOf(item) > -1));
        },
      };
    },
  },
  // {
  //   label: '租户',
  //   field: 'relTenantIds',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     mode: 'multiple',
  //     api: getAllTenantList,
  //     numberToString: true,
  //     labelField: 'name',
  //     valueField: 'id',
  //   },
  // },
  {
    label: userFields.userIdentity,
    field: 'userIdentity',
    component: 'RadioGroup',
    defaultValue: 1,
    componentProps: ({ formModel }) => {
      return {
        options: [
          { label: userFields.normalUser, value: 1, key: '1' },
          { label: userFields.leader, value: 2, key: '2' },
        ],
        onChange: () => {
          formModel.userIdentity == 1 && (formModel.departIds = []);
        },
      };
    },
  },
  {
    label: userFields.departIds,
    field: 'departIds',
    component: 'Select',
    componentProps: {
      mode: 'multiple',
    },
    ifShow: ({ values }) => values.userIdentity == 2,
  },
  {
    label: userFields.avatar,
    field: 'avatar',
    component: 'JImageUpload',
    componentProps: {
      fileMax: 1,
    },
  },
  {
    label: userFields.birthday,
    field: 'birthday',
    component: 'DatePicker',
  },
  {
    label: userFields.sex,
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: common('chooseText'),
      stringToNumber: true,
    },
  },
  {
    label: userFields.email,
    field: 'email',
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [{ ...rules.duplicateCheckRule('sys_user', 'email', model, schema, true)[0] }, { ...rules.rule('email', false)[0] }];
    },
  },
  {
    label: userFields.phone,
    field: 'phone',
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [
        { ...rules.duplicateCheckRule('sys_user', 'phone', model, schema, true)[0] },
        { pattern: /^1[3456789]\d{9}$/, message: common('inputRuleTip') + userFields.phone },
      ];
    },
  },
  {
    label: userFields.telephone,
    field: 'telephone',
    component: 'Input',
    rules: [{ pattern: /^0\d{2,3}-[1-9]\d{6,7}$/, message: common('inputRuleTip') + userFields.telephone }],
  },
  // {
  //   label: '工作流引擎',
  //   field: 'activitiSync',
  //   defaultValue: 1,
  //   component: 'JDictSelectTag',
  //   componentProps: {
  //     dictCode: 'activiti_sync',
  //     type: 'radio',
  //     stringToNumber: true,
  //   },
  // },
];

export const formPasswordSchema: FormSchema[] = [
  {
    label: userFields.username,
    field: 'username',
    component: 'Input',
    componentProps: { readOnly: true },
  },
  {
    label: userFields.password,
    field: 'password',
    component: 'StrengthMeter',

    rules: [
      {
        required: true,
      },
    ],
  },
  {
    label: userFields.confirmPassword,
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
];

export const formAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: userFields.username,
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: t('agentUserName'),
    required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 10,
    },
  },
  {
    field: 'startTime',
    label: t('agentStartTime'),
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: common('chooseText') + t('agentStartTime'),
    },
  },
  {
    field: 'endTime',
    label: t('agentEndTime'),
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: common('chooseText') + t('agentEndTime'),
    },
  },
  {
    field: 'status',
    label: userFields.status,
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

export const formQuitAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: userFields.username,
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: t('transferUserName'),
    required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 1,
    },
  },
  {
    field: 'startTime',
    label: t('transferStartTime'),
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: common('chooseText') + t('transferStartTime'),
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'endTime',
    label: t('transferEndTime'),
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: common('chooseText') + t('transferEndTime'),
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'status',
    label: userFields.status,
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

//租户用户列表
export const userTenantColumns: BasicColumn[] = [
  {
    title: userFields.username,
    dataIndex: 'username',
    width: 120,
  },
  {
    title: userFields.realname,
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: userFields.avatar,
    dataIndex: 'avatar',
    width: 120,
    customRender: render.renderAvatar,
  },
  {
    title: userFields.phone,
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: userFields.orgCode,
    width: 150,
    dataIndex: 'orgCodeTxt',
  },
  {
    title: userFields.status,
    dataIndex: 'status',
    width: 80,
    customRender: ({ text }) => {
      if (text === '1') {
        return t('normal');
      } else if (text === '3') {
        return t('underApproval');
      } else {
        return t('rejected');
      }
    },
  },
];

//用户租户搜索表单
export const userTenantFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'username',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '名字',
    field: 'realname',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: userFields.sex,
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: '请选择性别',
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
];
